package com.lzb.service;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lzb.common.easyui.datagrid.DataGridJsonDTO;
import com.lzb.common.exception.ServiceException;
import com.lzb.dao.mapper.MngUserMapper;
import com.lzb.domain.MngUser;
import com.lzb.model.MngUserModel;

@Service(MngUserService.BEAN_PACKAGE_NAME)
@Transactional(readOnly=true)
public class MngUserService {

	@Resource
	private MngUserMapper mngUserMapper;
	
	public static final String BEAN_PACKAGE_NAME = "com.lzb.service.MngUserService";
	
	/**
	 * 添加用户
	 * @param user
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void add(MngUser user) {
		try {
			mngUserMapper.insert(user);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public DataGridJsonDTO query(MngUserModel queryModel) {
		DataGridJsonDTO result = new DataGridJsonDTO();
		List<MngUser> _resultLst = mngUserMapper.getBeansByBeanForPager(queryModel);
		int count = mngUserMapper.getCount(queryModel);
		result.setRows(_resultLst);
		result.setTotal(count);
		return result;
	}
	
	public List<MngUserModel> getMngUsersByUUIDs(String... uuids) {
		List<MngUserModel> modelLst = null;
		if(uuids != null && uuids.length != 0) {
			modelLst = new LinkedList<MngUserModel>();
			List<MngUser> mngUserLst = mngUserMapper.getBeansByUUIDs(uuids);
			MngUserModel _model = null;
			int length = uuids.length;
			for(int i=0; i<length; i++) {
				_model = new MngUserModel();
				BeanUtils.copyProperties(mngUserLst.get(i), _model);
				modelLst.add(_model);
			}
		}
		return modelLst;
	}
	
	public MngUserModel login(MngUserModel queryModel) throws ServiceException {
		String name = queryModel.getName();
		String password = queryModel.getPwd();
		if(StringUtils.isBlank(name)) {
			throw new ServiceException("用户名不能为空串");
		}
		if(StringUtils.isBlank(password)) {
			throw new ServiceException("密码不能为空串");
		}
		//是否有该用户名的用户，有且仅有一个
		MngUser _mngUser = mngUserMapper.login(queryModel);
		if(_mngUser == null) {
			return null;
		}
		BeanUtils.copyProperties(_mngUser, queryModel);
		return queryModel;
	}
	
	public List<MngUserModel> findAll() {
		List<MngUser> mngUserLst = mngUserMapper.selectAll();
		List<MngUserModel> result = null;
		if(mngUserLst != null && 0 != mngUserLst.size()) {
			result = new LinkedList<MngUserModel>();
			MngUserModel _result = null;
			for(MngUser _mngUser : mngUserLst) {
				_result = new MngUserModel();
				BeanUtils.copyProperties(_mngUser, _result);
				result.add(_result);
			}
		}
		return result;
	}
	
}
